//
//  DropControl.m
//  CD1505Weibo
//
//  Created by 千锋 on 15/12/30.
//  Copyright (c) 2015年 Hawie. All rights reserved.
//

#import "DropControl.h"

@interface DropControl ()
@property(nonatomic,weak)UIView*sView;
@property(nonatomic,strong)UIImageView*dropImgView;
@property(nonatomic,assign)CGRect imgFrame;
@property(nonatomic,strong)UITableView *tableView;
@end
@implementation DropControl
-(instancetype)initWithInsideViewFrame:(CGRect)frame inView:(UIView*)view dataSource:(NSArray *)dataArray
{
    self=[super init];
    if (self) {
        self.sView=view;
        self.frame=[UIScreen mainScreen].bounds;
        self.backgroundColor=[UIColor redColor];
        self.imgFrame=frame;
        [self addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchDown];
        self.dropImgView=[[UIImageView alloc]init];
        [self addSubview:self.dropImgView];
        UIImage *img=[UIImage imageNamed:@"popover_background"];
        img=[img stretchableImageWithLeftCapWidth:5 topCapHeight:10];
        self.dropImgView.image=img;
        CGRect tableRect=frame;
        tableRect.origin.x=5;
        tableRect.origin.y=10;
        tableRect.size.width-=10;
        tableRect.size.height-=10;
        self.dropImgView.userInteractionEnabled=YES;
        self.tableView=[[UITableView alloc]initWithFrame:tableRect style:UITableViewStylePlain];
        [self.dropImgView addSubview:self.tableView];
    }
    return self;
}
-(void)show
{
    [self.sView addSubview:self];
//    CGFloat height =self.imgFrame.size.height;
    CGRect rect=self.imgFrame;
    rect.size.height=0;
    self.dropImgView.frame=rect;

    [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
         self.dropImgView.frame=self.imgFrame;
    } completion:^(BOOL finished) {
        
    }];
}
-(void)dismiss
{
    [self removeFromSuperview];
}
-(void)hide
{
    CGRect rect=self.imgFrame;
    rect.size.height=0;
    [UIView animateWithDuration:1.f animations:^{
        self.dropImgView.frame=rect;
    } completion:^(BOOL finished) {
        if (_afhblk) {
            _afhblk();
        }
    }];
   
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
